<?php
require_once "simple_html_dom.php";
require_once "const.php";
require_once 'logger/Logger.php';


Logger::configure('config.xml');


class VOASpider{
	private $numberPrePage = 60;
	function getlatestVOAList(){
		$rcList = array();
		$str = file_get_contents( "http://www.51voa.com/");
		$html = str_get_html($str);
		$ret = $html->find('div[id=list] ul li');
		foreach ($ret as $li){
			$links = $li->find("a");
			$rc = new VOARecord();
			foreach ($links as $a){
				if($a->find("img"))
					continue;
				$url=$a->href;
				$text = $a->plaintext;

				$index = stripos($text ,"[");
				if($index === 0){
					strlen($text);
					$catalog = explode(" ",$text);
 
					$newCatalog = array();
					for ($i =1;$i<count($catalog)-1;$i++){
					 $newCatalog[]= $catalog[$i];
					}
					$rc->catalog = join($newCatalog," ");
				}
				else{
					$rc->url = $url;
				}
				
			} 
			$rcList[]= $rc;
		}
		return $rcList; 
	}
	
	function getVOAStandardList($startIndex,$endIndex){
		$pagestartIndex = ($startIndex/$this->numberPrePage)+1;
		$pageEndIndex = ($endIndex/$this->numberPrePage)+1;
		for($i = $pagestartIndex;$i<=$pageEndIndex;$i++){
			
		}
	}
	
	function getVOAListByCatalog($catalog,$pageIndex){
		if (strcasecmp($catalog, "standard english")==0){
			$catalog = "VOA Standard";
		}
		$newCatalogs = join(explode(" ",$catalog),"_");
		$url = "http://www.51voa.com/{$newCatalogs}_{$pageIndex}.html";
		return $this->getVOAList($url,$catalog);
	}
	
	function getVOAList($url){
		$rcList = array();
		$str = file_get_contents( $url);
		$html = str_get_html($str);
		$ret = $html->find('div[id=list] ul li');
		foreach ($ret as $li){
			$links = $li->find("a");
			$rc = new VOARecord();
			foreach ($links as $a){
				if($a->find("img"))
					continue;
				$url=$a->href;
				$text = $a->plaintext;

				$index = stripos($text ,"[");
				if($index === 0){
					strlen($text);
					$catalog = explode(" ",$text);
		
					$newCatalog = array();
					for ($i =1;$i<count($catalog)-1;$i++){
						$newCatalog[]= $catalog[$i];
					}
					$rc->$album = join($newCatalog," ");
				}
				else{
					$rc->url = $url;
				}
		
			}
			$rcList[]= $rc;
		}
		return $rcList;
	}
	
}
class tingClassSpider{
	function __construct(){
		$this->logger = Logger::getLogger("main");
	}
 

	function getAudioList(){
		$this->logger->info("enter getAudioList");
		$rcList = array();
		set_time_limit(60) ;
		$opts = array(
				'http'=>array(
						'method'=>"GET",
						'timeout'=>60,
				)
		);
		$context = stream_context_create($opts);
		ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; GreenBrowser)');
 
		$str = @file_get_contents( "http://www.tingclass.net/voa/", false, $context);
		if($str=== false){
			$this->logger->error("get {$url} failed {$str}");
			return $rcList;
		}
		$html = str_get_html($str);
		
		$ret = $html->find('.ellipsis');
		if(count($ret)==0){
			$this->logger->error("get http://www.tingclass.net/voa/ failed {$str}");
			return $rcList;
		}
		foreach ($ret as $li){
			$links = $li->find("a");
			if(count($links)==0){
				$this->logger->error("get http://www.tingclass.net/voa/ failed {$str}");
				continue;
			}
			$rc = new Music();
			foreach ($links as $a){
				$url=$a->href;
				$pos = strpos($url,"show");
				if ($pos === false)
					continue;
				$url = substr_replace($url,"down",$pos,4);
				set_time_limit(60) ;
				//sleep(2);
				$str = @file_get_contents($url, false, $context);
				if($str=== false){
					$this->logger->error("get {$url} failed {$str}");
					continue;
				}
				$html = str_get_html($str);
				$urls =$this->getUrl($html);
				if(count($urls)==0){
					$this->logger->error("get {$url} failed {$str}");
					continue;
				}
				$rc->url = $urls[0];
				$rc->album = $this->getAlbum($html);
				$rs = $this->getFamily($html);
				$rc->tags = $rs[0];
				$rc->name = $rs[1];
			}
			$rcList[]= $rc;

		}
		return $rcList;
	}
	
	function getUrl($html){
		$urls = array();
		$ret = $html->find('.download');
		foreach ($ret as $li){
			$links = $li->find("a");
			$rc = new Music();
			foreach ($links as $a){
				$urls[]= $url=$a->href;
			}
		}
		return $urls;
	}
	
	
	function getFamily($html){
		$ret = $html->find('.titleLong');
		 
		$texts = explode(":",$ret[0]->plaintext);
		$tags = array();
		$tags[] = array_shift($texts);
		$tags[] =  array_shift($texts);
		$title = join(":",$texts);
		return array( $tags,$title);
	}
 
	
	function getAlbum($html){
		$album = "unknown";
		$ret = $html->find('.shuoshu');
		foreach ($ret as $li){
			$links = $li->find("a");
			$rc = new Music();
			foreach ($links as $a){
				$album=$a->plaintext;
			}
		}
		return $album;
		
	}
	
} 

class Music {
	public $album;
	public $name;
	public $albumCover;
	public $cover;
	public $tags =array();
	public $url;
	public $lrc;
}
	$voa = new tingClassSpider();
	$rs = $voa->getAudioList();
	$repsondTxt = json_encode($rs);
	$f = fopen("music.json", 'w');
	fwrite($f, $repsondTxt);
	fclose($f);
	echo $repsondTxt;
 
?>